package com.ict.txmsv2.services;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ict.txmsv2.mapper.RecordMapper;
import com.ict.txmsv2.model.Record;
import com.ict.txmsv2.model.RespPageBean;
import com.ict.txmsv2.model.WebLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-16
 */
@Service
public class RecordService {

    @Autowired
    RecordMapper recordMapper;

    public RespPageBean getAllLog(Integer page, Integer size, Record record) {
        if (page != null&& page>0){
            page = (page-1)*size;
        }
        RespPageBean respPageBean = new RespPageBean();
        List<Record> records = recordMapper.getAllLog(page,size,record);
        if (CollectionUtil.isNotEmpty(records)){
            for (Record r : records) {
                String content = r.getInfo();
                if (StringUtils.isNotEmpty(content)){
                    WebLog webLog = JSONUtil.toBean(content,WebLog.class);
                    r.setWebLog(webLog);
                    r.setInfo(null);
                }
            }
        }
        respPageBean.setData(records);
        respPageBean.setTotal(recordMapper.getTotalSeletive(record));
        return respPageBean;
    }

    public void addRecord(Record record) {
        recordMapper.insertSelective(record);
    }
}
